﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace _47WPFDataBindingIValueConverter
{
    [ValueConversion(typeof(double), typeof(double))]
    public class SquareRootConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double number;
            if (double.TryParse(value.ToString(), out number)) 
            {
                return Math.Sqrt(number);            
            }
            return DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double number;
            if (double.TryParse(value.ToString(), out number))
            {
                return number*number;
            }
            return DependencyProperty.UnsetValue;
        }
    }
}
